package udp_tcp.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class LoginDialog extends JDialog {
    private JTextField usernameField;
    private JTextField portField;
    private JButton confirmButton;

    public LoginDialog(Frame parent) {
        super(parent, "登录", true);
        setSize(300, 200);
        setLocationRelativeTo(parent);
        setLayout(new BorderLayout());

        // 表单面板
        JPanel formPanel = new JPanel(new GridLayout(2, 2, 5, 5));
        formPanel.add(new JLabel("用户名:"));
        usernameField = new JTextField();
        formPanel.add(usernameField);

        formPanel.add(new JLabel("端口号:"));
        portField = new JTextField("12345");
        formPanel.add(portField);

        add(formPanel, BorderLayout.CENTER);

        // 按钮面板
        confirmButton = new JButton("确定");
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(confirmButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    public void setConfirmAction(Action action) {
        confirmButton.setAction(action);
    }

    public String getUsername() {
        return usernameField.getText().trim();
    }

    public int getPort() {
        try {
            return Integer.parseInt(portField.getText().trim());
        } catch (NumberFormatException e) {
            return -1;
        }
    }
}